//选择地址
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hxdj/common/base_page/my_loading_page.dart';
import 'package:hxdj/common/components/my_tap_widget.dart';
import 'package:hxdj/common/components/my_text.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/home/address/edit_address/edit_address_viewmodel.dart';
import 'package:hxdj/home/address/loca_address/loca_address_page.dart';
import 'package:provider/provider.dart';

//编辑地址
class EditAddressPage extends StatefulWidget {
  final String addressId;

  const EditAddressPage({super.key, this.addressId = ""});
  static String routeName = "/EditAddressPage";

  @override
  State<EditAddressPage> createState() => _EditAddressPageState();
}

class _EditAddressPageState extends State<EditAddressPage> {
  late EditAddressViewModel viewModel;

  @override
  void initState() {
    super.initState();

    viewModel = context.read<EditAddressViewModel>();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.context = context;
      viewModel.getAddressDetail(widget.addressId);
    });
  }

  @override
  dispose() {
    super.dispose();
  }
  
  //编辑名字
  _buildName() {
    return Row(
      children: [
        SizedBox(width: 70.w, child: MyText("联系人", textStyle: MyTextStyles.textBlackBold14)),
        Expanded(
          child: Container(
            decoration: const BoxDecoration(
                border: Border(
              bottom: BorderSide(color: MyColours.line, width: 1),
            )),
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: viewModel.nameController,
                    onChanged: (value) {
                      viewModel.checkInput();
                    },
                    decoration: const InputDecoration(
                      border: InputBorder.none,
                      hintText: "请输入联系人姓名",
                      hintStyle: TextStyle(color: MyColours.textGray),
                    ),
                  ),
                ),
                MyTapWidget(
                  onTap: () {
                    viewModel.selectSex(1);
                  },
                  child: Container(
                      margin: EdgeInsets.only(left: 10.w),
                      child: Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: MyImage(
                              imageName: viewModel.sex == 1 ? "ic_select" : "ic_un_select",
                              width: 20.w,
                            ),
                          ),
                          const MyText("先生", color: MyColours.textGray),
                        ],
                      )),
                ),
                MyTapWidget(
                  onTap: () {
                    viewModel.selectSex(2);
                  },
                  child: Container(
                      margin: const EdgeInsets.only(left: 10),
                      child: Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(right: 5),
                            child: MyImage(
                              imageName: viewModel.sex == 2 ? "ic_select" : "ic_un_select",
                              width: 20,
                            ),
                          ),
                          const MyText(
                            "女士",
                            color: MyColours.textGray,
                          ),
                        ],
                      )),
                ),
              ],
            ),
          ),
        )
      ],
    );
  }

  //编辑手机号
  _buildPhone() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Row(
        children: [
          SizedBox(width: 70.w, child: MyText("手机号", textStyle: MyTextStyles.textBlackBold14)),
          Expanded(
            child: TextField(
              controller: viewModel.phoneController,
              inputFormatters: [LengthLimitingTextInputFormatter(11), FilteringTextInputFormatter.digitsOnly],
              onChanged: (value) {
                viewModel.checkInput();
              },
              decoration: const InputDecoration(
                border: InputBorder.none,
                hintText: "请输入手机号",
                hintStyle: TextStyle(color: MyColours.textGray),
              ),
            ),
          ),
        ],
      ),
      const MyText(
        "温馨提示：请确保当前手机号可以联系到您哦～",
        color: MyColours.appMain,
        fontSize: 13,
      ),
    ]);
  }

  //服务地址
  _buildServiceAddress() {
    return Row(
      children: [
        SizedBox(width: 70.w, child: MyText("服务地址", textStyle: MyTextStyles.textBlackBold14)),
        Expanded(
          child: Container(
            decoration: const BoxDecoration(
                border: Border(
              bottom: BorderSide(color: MyColours.line, width: 1),
            )),
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    readOnly: true,
                    controller: viewModel.addressController,
                    onChanged: (value) {
                      viewModel.checkInput();
                    },
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: "请选择服务地址",
                      hintStyle: TextStyle(
                          color: viewModel.addressController.text.isNotEmpty ? MyColours.text : MyColours.textGray),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        GestureDetector(
            onTap: () {
              NavigatorUtils.pushParams(context, LocaAddressPage.routeName);
            },
            child: Container(
                padding: EdgeInsets.all(10.w),
                child: const MyImage(imageName: "ic_arrow_right", width: 5, height: 8))),
      ],
    );
  }

  //门牌号
  _buildHouseNumber() {
    return Row(
      children: [
        SizedBox(width: 70.w, child: MyText("门牌号", textStyle: MyTextStyles.textBlackBold14)),
        Expanded(
          child: TextField(
            controller: viewModel.doorController,
            onChanged: (value) {
              viewModel.checkInput();
            },
            decoration: const InputDecoration(
              border: InputBorder.none,
              hintText: "例：108号楼307室",
              hintStyle: TextStyle(color: MyColours.textGray),
            ),
          ),
        )
      ],
    );
  }

  //设置wei默认
  _buildDefaultAddress() {
    return MyTapWidget(
      onTap: () {
        viewModel.setDefault();
      },
      child: Container(
          color: Colors.white,
          margin: const EdgeInsets.only(left: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Padding(
                padding: const EdgeInsets.only(right: 10),
                child: MyImage(
                  imageName: viewModel.isDefault ? "ic_select" : "ic_un_select",
                  width: 20,
                ),
              ),
              const MyText(
                "设为默认地址",
                color: MyColours.textGray,
              ),
            ],
          )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (context, EditAddressViewModel? model, Widget? child) {
      /*如果是有地址, 就要
         * 1. 获取地址信息
         * 2. 显示地址信息
         * 3. 显示编辑按钮
         * 4. 显示删除按钮
         * 5. 显示保存按钮
         * 6. 显示取消按钮
         * 7. 显示保存并使用按钮
         * 8. 显示编辑地址的标题
         * 9. 显示编辑地址的副标题
         * 10. 显示编辑地址的副标题
         * 11. 显示编辑地址的副标题
         * 
         */

      if (model?.state == ViewState.loading) {
        return Scaffold(
          backgroundColor: MyColours.bgColor,
          appBar: MyAppBar(
            title: CommonPlugin.isEmpty(widget.addressId) ? "新增地址" : "编辑地址",
          ),
          body: const MyLoadingWidget(),
        );
      }

      return Scaffold(
          appBar: MyAppBar(
            title: CommonPlugin.isEmpty(widget.addressId) ? "新增地址" : "编辑地址",
              rightWidget: CommonPlugin.isEmpty(widget.addressId)
                  ? Container()
                  : MyTapWidget(
                onTap: () {
                  viewModel.deleteAddress(widget.addressId);
                },
                      child: const MyText("删除", color: MyColours.appMain))
                 
          ),
          body: SingleChildScrollView(
              child: Column(children: [
            Container(
              margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 15, bottom: 6.5),
              child: MyBorder(
                  radius: 17.w,
                  child: Container(
                    margin: EdgeInsets.all(13.w),
                    child: Column(
                      children: [
                        //名字
                        _buildName(),
                        //手机号
                        _buildPhone(),
                      ],
                    ),
                  )),
            ),
            Container(
              margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 15, bottom: 6.5),
              child: MyBorder(
                  radius: 17.w,
                  child: Container(
                    margin: EdgeInsets.all(13.w),
                    child: Column(
                      children: [
                        //服务地址
                        _buildServiceAddress(),

                        //门牌号
                        _buildHouseNumber(),

                        //设置wei默认
                        _buildDefaultAddress(),
                      ],
                    ),
                  )),
            ),
            GestureDetector(
              onTap: () async {
                viewModel.saveAddress();
              },
              child: Container(
                margin: const EdgeInsets.only(top: 30),
                padding: const EdgeInsets.symmetric(horizontal: 55),
                child: Container(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  decoration: BoxDecoration(
                    color: viewModel.isSave ? MyColours.appMain : MyColours.appMain.withOpacity(0.6),
                    //设置四周圆角 角度
                    borderRadius: BorderRadius.circular(30),
                  ),
                  alignment: Alignment.center,
                  child: Text("保存并使用",
                      style: TextStyle(fontSize: 15.sp, color: Colors.white, fontWeight: FontWeight.w700)),
                ),
              ),
            ),
          ])));
    });
  }

  
}
